#include "uninstallitem.h"

#include <QDebug>
#include <QVersionNumber>
#include <gsettingmonitor.h>

#include "include/common.h"
#include "include/globalsignal.h"

UninstallItem::UninstallItem(HardWareInfo hardwareinfo, QWidget *parent)
        : QWidget(parent),
          m_hardwareinfo(hardwareinfo),
          m_check(new QCheckBox()),
          m_Icon(new kdk::KPressButton()),
          m_deviceType(new QLabel()),
          m_deviceName(new K::TextLabel()),
          m_driverVersion(new QLabel()),
          m_uninstallButton(new QPushButton()),
          m_progressBar(new QProgressBar()),
          m_btnStackWidget(new QStackedWidget),
          m_uninstallingLayout(new QHBoxLayout()),
          m_uninstallingWidget(new QWidget()),
          m_mainLayout(new QHBoxLayout()),
          m_typeNameLayout(new QVBoxLayout()),
          m_typeNameWidget(new QWidget()) {
    initGui();
    initConnect();
}

void UninstallItem::initGui() {
    this->setFixedHeight(ITEMHEIGET);

    m_Icon->setButtonType(kdk::KPressButton::CircleType);
    m_Icon->setFixedSize(DEVICEICONBUTTONSIZE);
    if (!m_Icon->isChecked()) {
        m_Icon->click();
    }
    switch (m_hardwareinfo.type) {
        case DeviceType::SCANNER:
            m_deviceType->setText(tr("Scanner"));
            m_Icon->setIcon(getIcon("ukui-scanner-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::GraphicCard:
            m_deviceType->setText(tr("GraphicCard"));
            m_Icon->setIcon(getIcon("ukui-graphics-card-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::SoundCard:
            m_deviceType->setText(tr("SoundCard"));
            m_Icon->setIcon(getIcon("audio-volume-high-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::NetworkCard:
            m_deviceType->setText(tr("NetworkCard"));
            m_Icon->setIcon(getIcon("ukui-zs-network-card-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::Bluetooth:
            m_deviceType->setText(tr("Bluetooth"));
            m_Icon->setIcon(getIcon("bluetooth-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::PRINTER:
            m_deviceType->setText(tr("Printer"));
            m_Icon->setIcon(getIcon("printer-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::InputDev:
            m_deviceType->setText(tr("Input Device"));
            m_Icon->setIcon(getIcon("ukui-zs-sidebar-yjjc-symbolic", DEVICEICONBUTTONSIZE));
            break;
        case DeviceType::VideoOrAudio:
            m_deviceType->setText(tr("Video Device"));
            m_Icon->setIcon(getIcon("camera-video-symbolic", DEVICEICONBUTTONSIZE));
            break;
        default:
            m_deviceType->setText(tr("Other Device"));
            m_Icon->setIcon(getIcon("ukui-bf-operation-log-symbolic", DEVICEICONBUTTONSIZE));
            break;
    }
    m_Icon->setIconSize(DEVICEICONSIZE);
    m_check->setEnabled(false);

    m_deviceName->setFixedWidth(200);
    m_deviceName->SetText(m_hardwareinfo.model);

    // 判断是否有驱动可以卸载
    if (!m_hardwareinfo.upgradeable){
        QString name = m_hardwareinfo.driver_now;
        QStringList namelist;
        QString driver = m_hardwareinfo.driver_version_now;
        QStringList driverlist;
        if (m_hardwareinfo.driver_now.contains(";")) {
            namelist = name.split(";");
            namelist.removeLast();
            name = " ";
            if (m_hardwareinfo.driver_version_now.contains(";")){
                driverlist = driver.split(";");
                driverlist.removeLast();
                driver = " ";
            }
            for (int i = 0; i < namelist.length(); ++i) {
                name = tr("Version: ") + namelist[i] + " ";
            }
        }else{
            name = tr("Version: ") + name;
        }
        m_driverVersion->setText(name);
    }
    m_uninstallButton->setFixedSize(UPDATEBTNSIZE);
    m_uninstallButton->setText(tr("Uninstall"));


    m_progressBar->setMinimum(0);
    m_progressBar->setMaximum(100);
    m_progressBar->setTextVisible(false);
    m_progressBar->setFixedSize(PROGRESSBARSIZE);

    m_typeNameLayout->addWidget(m_deviceType);
    m_typeNameLayout->addWidget(m_deviceName);
    m_typeNameLayout->setContentsMargins(0, 0, 0, 0);
    m_typeNameLayout->setSpacing(0);
    m_typeNameWidget->setLayout(m_typeNameLayout);

    m_uninstallingLayout->setContentsMargins(0, 0, 0, 0);
    m_uninstallingLayout->setSpacing(0);
    m_uninstallingLayout->addWidget(m_progressBar);
    m_uninstallingWidget->setLayout(m_uninstallingLayout);

    m_btnStackWidget->setContentsMargins(0, 0, 0, 0);
    m_btnStackWidget->addWidget(m_uninstallButton);
    m_btnStackWidget->addWidget(m_uninstallingWidget);
    m_btnStackWidget->setCurrentWidget(m_uninstallButton);

    m_mainLayout->setContentsMargins(0, 16, 0, 16);
    m_mainLayout->setSpacing(0);
    m_mainLayout->addWidget(m_check);
    m_mainLayout->addSpacing(14);
    m_mainLayout->addWidget(m_Icon);
    m_mainLayout->addSpacing(12);
    m_mainLayout->addWidget(m_typeNameWidget);
    m_mainLayout->addSpacing(24);
    m_mainLayout->addStretch();
    m_mainLayout->addWidget(m_driverVersion, 2, Qt::AlignLeft);
    m_mainLayout->addStretch();
    m_mainLayout->addSpacing(20);
    m_mainLayout->addWidget(m_btnStackWidget);

    this->setLayout(m_mainLayout);
}

void UninstallItem::initConnect() {
    connect(m_uninstallButton, &QAbstractButton::clicked, this, &UninstallItem::uninstallBtnClicked);
    connect(m_Icon, &kdk::KPressButton::clicked, [=](){m_Icon->setChecked(true);});
    connect(g_signal, &GlobalSignal::themeChangeSignal, this, &UninstallItem::themeChangeSlot);
}

void UninstallItem::resizeEvent(QResizeEvent *event) {
    Q_UNUSED(event);
    int labelWidth = 200 + this->width() - 678;
    if (labelWidth <= 400) {
        m_deviceName->setFixedWidth(labelWidth);
    } else {
        m_deviceName->setFixedWidth(400);
    }
}

DriverInfo UninstallItem::getGpuDriver() {
    // 多个驱动信息
    QList<DriverInfo> driverNameList = m_hardwareinfo.driverNameList;
    DriverInfo driver = driverNameList.at(0);
    // 挑选最新驱动包
    for (int i = 1; i < driverNameList.length(); ++i) {
        QVersionNumber v1 = QVersionNumber::fromString(driver.version);
        QVersionNumber v2 = QVersionNumber::fromString(driverNameList.at(i).version);
        if (v2 > v1) {
            driver = driverNameList.at(i);
        }
    }
    return driver;
}

void UninstallItem::uninstallBtnClicked() {
    m_btnStackWidget->setCurrentWidget(m_uninstallingWidget);
    QString driver = m_hardwareinfo.driver_now;
    QStringList drivers;
    if(driver.contains(";")){
        drivers = driver.split(";");
    }
    drivers.removeLast();

    emit g_signal->uninstallDriver(this, drivers);
}

void UninstallItem::themeChangeSlot() {
    float fontSize = kdk::GsettingMonitor::getSystemFontSize().toFloat();
    QFont font;
    font.setPointSize(fontSize);
    m_deviceType->setFont(font);
    m_deviceName->setFont(font);
    m_driverVersion->setFont(font);
    m_uninstallButton->setFont(font);
}

QString UninstallItem::getDevName() {
    return m_hardwareinfo.model;
}


DeviceType UninstallItem::getDevType() {
    return m_hardwareinfo.type;
}

void UninstallItem::changeProgress(int percent) {
    m_progressBar->setValue(percent);
}

QIcon UninstallItem::getIcon(QString iconName, QSize iconSize)
{
    QString path = "/usr/share/kylin-os-manager/icons/";

    QIcon icon;
    if (iconName.indexOf("/") != -1) {
        icon.addFile(iconName, iconSize);
        return icon;
    }

    icon = QIcon::fromTheme(iconName);
    if (!icon.isNull())
        return icon;

    QString selfIconPath = path + iconName + ".svg";
    icon.addFile(selfIconPath, iconSize);
    return icon;
}
